package com.api.qfengx.data.field;

import com.alibaba.fastjson.JSON;
import com.weaver.qfengx.DaoUtils;
import com.weaver.qfengx.NumberUtils;
import com.weaver.qfengx.RequestUtils;
import com.weaver.qfengx.StringUtils;
import com.weaver.qfengx.config.DevModeConfig;
import com.weaver.qfengx.entity.Result;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * 字段类型查询
 * @Author Opp <qffg1998@126.com>
 * @Date 2023-03-11 21:59
 */
@Path("/qfengx/data/field")
public class FieldDataApi {


    @GET
    @Path("/selectItems")
    @Produces(MediaType.APPLICATION_JSON)
    public String listAll(@Context HttpServletRequest request) {
        try {
            Map<String, Object> paramsMap = RequestUtils.getParamsMap(request);
            String queryId = StringUtils.val(paramsMap.get("queryId"));
            if (StringUtils.isBlank(queryId)) {
                return JSON.toJSONString(Result.fail("queryId为空"));
            }
            String tablename = DevModeConfig.getPropConfig("data.field.select.tblname." + queryId, "").getValue();
            if (StringUtils.isBlank(tablename)) {
                return JSON.toJSONString(Result.fail("tblname未配置"));
            }
            String fieldName = DevModeConfig.getPropConfig("data.field.select.name." + queryId, "").getValue();
            if (StringUtils.isBlank(tablename)) {
                return JSON.toJSONString(Result.fail("fieldname未配置"));
            }
            String all = StringUtils.val(paramsMap.get("all"), "");
            String appendSql = " and cancel = 0";
            if (StringUtils.isNotEmpty(all)) {
                appendSql = "";
            }
            return JSON.toJSONString(Result.ok(DaoUtils.executeQueryToMapList("select selectname, selectvalue from workflow_SelectItem where fieldid = " +
                    "( select id from workflow_billfield where BILLID = (select id from workflow_bill " +
                    "where TABLENAME = ?) and fieldname = ? ) " + appendSql, tablename, fieldName)));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }

}
